<?php
namespace Admin\Controller;

class CategoryController extends BaseController
{
    const CATEGORY_TYPE_NORMAIL = 1;
    const CATEGORY_TYPE_SINGLE_PAGE = 2;
    const CATEGORY_TYPE_LINK = 3;

	public function indexAction() 
    {
		//分类树
		vendor('Phpcms.Tree', '', '.class.php');
		$Tree = new \Tree();
		$Tree->icon = array('&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ ');
		$Tree->nbsp = '&nbsp;&nbsp;&nbsp;';
		
		$Category = M('Category');
		$category_list = $Category->order('list_order asc, id asc')->select();
		foreach ($category_list as $key => $val) {
		    switch ($val['type']) {
                case self::CATEGORY_TYPE_NORMAIL:
                    $category_list[$key]['type_alias'] = '内部栏目';
                    $category_list[$key]['str_manage'] = '<a href="' . U("Category/add", array('parent_id' => $val['id'])) . '">添加子栏目</a> | <a href="' . U("Category/update", array('id' => $val['id'])) . '">修改</a> | <a href="' . U('Category/delete', array('id' => $val['id'])) . '">删除</a>';
                    break;
                case self::CATEGORY_TYPE_SINGLE_PAGE:
                    $category_list[$key]['type_alias'] = '<span style="color:blue">单网页</span>';
                    $category_list[$key]['str_manage'] = '<a href="' . U("Category/addSinglePage", array('parent_id' => $val['id'])) . '">添加子栏目</a> | <a href="' . U("Category/updateSinglePage", array('id' => $val['id'])) . '">修改</a> | <a href="' . U('Category/delete', array('id' => $val['id'])) . '">删除</a>';
                    break;
                case self::CATEGORY_TYPE_LINK:
                    $category_list[$key]['type_alias'] = '<span style="color:green">外部链接</span>';
                    $category_list[$key]['str_manage'] = '<a href="' . U("Category/addLink", array('parent_id' => $val['id'])) . '">添加子栏目</a> | <a href="' . U("Category/updateLink", array('id' => $val['id'])) . '">修改</a> | <a href="' . U('Category/delete', array('id' => $val['id'])) . '">删除</a>';
                    break;
		    }
		}
		$str  = "<tr>
					<td align='center'><input name='list_order[\$item[id]]' type='text' size='3' value='\$item[list_order]' class='input-text-c input-text'></td>
					<td align='center'>\$item[id]</td>
					<td >\$spacer\$item[name]</td>
					<td>\$item[type_alias]</td>
					<td>\$item[str_manage]</td>
				</tr>";
		$Tree->init($category_list);
		$category_list = $Tree->get_tree(0, $str);		
		$this->assign('category_list', $category_list);
		$this->display();	
	}	
	
	public function addAction() 
    {
		if (IS_POST) {
			$Category = D('Category');
            $data = $_POST['Category'];
            $data['type'] = self::CATEGORY_TYPE_NORMAIL;
			if ($Category->create($data)) {
				if (false !== $Category->add()) {
					$this->success('添加成功！');
				} else {
					$this->error('添加失败!');
				}
			} else {
				$this->error($Category->getError()); 
			}
		} else {
			$parent_id = isset($_GET['parent_id']) ? intval($_GET['parent_id']) : 0;
			
			//分类树
			vendor('Phpcms.Tree', '', '.class.php');
			$Tree = new \Tree();
		
			$Category = M('Category');
			$category_list = $Category->order('list_order asc, id asc')->select();
			foreach ($category_list as $key => $val) {
				$category_list[$key]['selected'] = $val['id'] == $parent_id ? 'selected' : '';
			}

			$str  = "<option value='\$item[id]' \$item[selected]>\$spacer \$item[name]</option>";
			$Tree->init($category_list);
			$category_list = $Tree->get_tree(0, $str);		
			$this->assign('category_list', $category_list);
			$this->display();
		}
	}
	
	public function updateAction() 
    {
		if (IS_POST) {
			$Category = D('Category');
            $data = $_POST['Category'];
            $data['type'] = self::CATEGORY_TYPE_NORMAIL;
			if ($Category->create($data)) {
				if (false !== $Category->save()) {
					$this->success('修改成功！', U('Category/index'));
				} else {
					$this->error('修改失败!');
				}
			} else {
				$this->error($Category->getError()); 
			}
		} else {
            $id = I('get.id', 0, 'intval');
			$Category = M('Category');
			$category_info = $Category->where('id = ' . $id)->order('list_order asc, id asc')->find();
			
			//分类树
			vendor('Phpcms.Tree', '', '.class.php');
			$Tree = new \Tree();
			$category_list = $Category->select();
			foreach ($category_list as $key => $val) {
				$category_list[$key]['selected'] = $val['id'] == $category_info['parent_id'] ? 'selected' : '';
			}
			$str  = "<option value='\$item[id]' \$item[selected]>\$spacer \$item[name]</option>";
			$Tree->init($category_list);
			$category_list = $Tree->get_tree(0, $str);		
			
			$this->assign('category_list', $category_list);
			$this->assign('category_info', $category_info);
			$this->display();
		}
	}

	public function addSinglePageAction()
    {
        if (IS_POST) {
            $Category = D('Category');
            $data = $_POST['Category'];
            $data['type'] = self::CATEGORY_TYPE_SINGLE_PAGE;
            if ($Category->create($data)) {
                if (false !== $Category->add()) {
                    $this->success('添加成功！');
                } else {
                    $this->error('添加失败!');
                }
            } else {
                $this->error($Category->getError());
            }
        } else {
            $parent_id = isset($_GET['parent_id']) ? intval($_GET['parent_id']) : 0;

            //分类树
            vendor('Phpcms.Tree', '', '.class.php');
            $Tree = new \Tree();

            $Category = M('Category');
            $category_list = $Category->order('list_order asc, id asc')->select();
            foreach ($category_list as $key => $val) {
                $category_list[$key]['selected'] = $val['id'] == $parent_id ? 'selected' : '';
            }

            $str  = "<option value='\$item[id]' \$item[selected]>\$spacer \$item[name]</option>";
            $Tree->init($category_list);
            $category_list = $Tree->get_tree(0, $str);
            $this->assign('category_list', $category_list);
            $this->display();
        }
    }

    public function updateSinglePageAction()
    {
        if (IS_POST) {
            $Category = D('Category');
            $data = $_POST['Category'];
            $data['type'] = self::CATEGORY_TYPE_SINGLE_PAGE;
            if ($Category->create($data)) {
                if (false !== $Category->save()) {
                    $this->success('修改成功！', U('Category/index'));
                } else {
                    $this->error('修改失败!');
                }
            } else {
                $this->error($Category->getError());
            }
        } else {
            $id = I('get.id', 0, 'intval');
            $Category = M('Category');
            $category_info = $Category->where('id = ' . $id)->order('list_order asc, id asc')->find();

            //分类树
            vendor('Phpcms.Tree', '', '.class.php');
            $Tree = new \Tree();
            $category_list = $Category->select();
            foreach ($category_list as $key => $val) {
                $category_list[$key]['selected'] = $val['id'] == $category_info['parent_id'] ? 'selected' : '';
            }
            $str  = "<option value='\$item[id]' \$item[selected]>\$spacer \$item[name]</option>";
            $Tree->init($category_list);
            $category_list = $Tree->get_tree(0, $str);

            $this->assign('category_list', $category_list);
            $this->assign('category_info', $category_info);
            $this->display();
        }
    }
	
	public function deleteAction() 
    {
        $id = I('get.id', 0, 'intval');
        $Category = M('Category');
        $list = $Category->where(array('parent_id' => $id))->select();
        if (!empty($list)) {
            $this->error('该菜单下有子菜单，请先删除子菜单！');
        }
        
		$res = M('Category')->delete($id);
		if (false !== $res) {
			$this->success('删除成功！', U('Category/index'));	
		} else {
			$this->error('删除失败!');	
		}
	}
	
	public function listOrderAction()
	{
		$Category = D('Category');
		$arr_list_order = $_POST['list_order'];
		foreach ($arr_list_order as $key => $val) {
			$data = array('list_order' => intval($val));
			$Category ->where('id = ' . intval($key))->setField($data);
		}
		$this->redirect('index');
	}


    /**
     * 编辑器上传处理
     */
    public function uploadJsonAction()
    {
        $config = array(
            'savePath' => C('PICTURE_UPLOAD.rootPath')  . 'category/',
            'subPath' => 'category/',
        );
        vendor('Kindeditor.UploadJson', '', '.class.php');
        $UploadJson = new \UploadJson($config);
        $UploadJson->upload();
    }

    /**
     * 编辑器上传文件管理
     */
    public function fileManagerJson()
    {

    }
}